package com.tlb.ejb;


import com.tlb.dto.Sticky;
import java.util.*;
import javax.persistence.*;

public class StickyDAO {
    private static com.tlb.web.Util util = com.tlb.web.Util.getUtil();
    public StickyDAO() {
    }
    
    
    public static void main(String args[]) {        
    }
    
    public Sticky getStickyByThreadId(int threadId) {
        try {
            EntityManager em = util.getPersistenceFactory().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            tx.begin();
            
            Sticky s = (Sticky)(em.createQuery(
                    "select s from Sticky s where threadId = :threadId"
                    )
                    .setParameter("threadId", threadId)
                    .getSingleResult());
            
            tx.commit();
            em.close();
            
            if(s != null) {
                return s;
            }
            
        } catch(Exception e) {
            System.err.println("Error getting sticky by thread: " + threadId);
            e.printStackTrace();
        }
        
        return null;
    }
    
    public void updateSticky(Sticky s) {
        try {
            EntityManager em = util.getPersistenceFactory().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            tx.begin();
            
            em.merge(s);
            
            tx.commit();
            em.close();
            
        } catch(Exception e) {
            System.out.println("Exception updating sticky: " + s.getStickyId() + " / threadId: " + s.getThreadId());
            e.printStackTrace();
        }
    }
    
    public void deleteStickyByThreadId(int threadId) {
        try {
            EntityManager em = util.getPersistenceFactory().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            tx.begin();
            
            
            em.createNativeQuery("delete from sticky where threadId = :threadId")
                .setParameter("threadId", threadId)
                .executeUpdate();
                
            
            tx.commit();
            em.close();
            
        } catch(Exception e) {
            System.out.println("Exception updateStickyByThreadId: " + threadId + " / ex: " + e.getMessage());
            e.printStackTrace();
        }
    }
    public void insertAdminSticky(int threadId, int posterId) {
        try {
            EntityManager em = util.getPersistenceFactory().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            tx.begin();
            
            
            em.createNativeQuery("insert into sticky(threadId, startDate, active, posterId) values (:threadId, now(), 1, :posterId);")
                .setParameter("threadId", threadId)
                .setParameter("posterId", posterId)
                .executeUpdate();
                
            
            tx.commit();
            em.close();
            
        } catch(Exception e) {
            System.out.println("Exception insertAdminSticky: " + threadId + " / ex: " + e.getMessage());
            e.printStackTrace();
        }
    }

    
}


